﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Battleship_569.Data
{
    class BattleGridManager
    {
        private int[,] battleGrid;
        private int[,] missleGrid;
        private List<Battleship> ships;

        // Fire Missle - this fires a missle at the grid and detects a hit.
        public bool fireMissle(int x, int y)
        {
            bool hit = false;
            missleGrid[x, y] = 9;
            if (getShip(x, y) > 0 && getShip(x, y) < 8)
            {
                hit = true;
                missleGrid[x, y] = 8;
                if (getShip(x, y) > 0 && getShip(x, y) < 6)
                {
                    //System.Windows.Forms.MessageBox.Show("ouch you hit a " + getShip(x,y));
                    // TinNT 
                    for (int i = 0; i < ships.Count; i++)
                    {
                        if (ships[i].getType() == getShip(x, y))
                        {
                            ships[i].setHit();
                        }
                    }
                }
            }
            return hit;
        }

        // TinNT - Set Ships from Form to this Grid

        public void setListShips(List<Battleship> ships)
        {
            this.ships = ships;
        }

        // TinNT - Get Ships from Grid to Form for send
        public List<Battleship> getListShips()
        {
            return ships;
        }

        //getShip this gets what ship is hidden on the grid.
        public int getShip(int x, int y)
        {
            return battleGrid[x, y];
        }

        // get battleship
        public Battleship getBattleShip(int ship)
        {
            return ships[ship];
        }

        #region Add ship into BattleGrid[,]
        // TinNT - Add Ship into Grid
        public void addShip(Battleship ship)
        {
            if (ship.getDirection() == "down")
            {
                addShipDown(ship.get_X(), ship.get_Y(), ship);
            }
            else if (ship.getDirection() == "across")
            {
                addShipAcross(ship.get_X(), ship.get_Y(), ship);
            }
            else if (ship.getDirection() == null)
            {
                addShipType1(ship.get_X(), ship.get_Y(), ship);
            }

        }
        #endregion

        // add the ship down to the grid
        public void addShipDown(int x, int y, Battleship ship)
        {
            int downX = x;
            // check down!
            for (int count = 0; count < ship.getLength(); count++)
            {
                setShip(downX, y, ship.getType());
                downX++;
            }
        }

        public void addShipType1(int x, int y, Battleship ship)
        {
            int downX = x;
            setShip(downX, y, ship.getType());
        }

        // add the ship across the grid
        public void addShipAcross(int x, int y, Battleship ship)
        {
            int downX = x;
            for (int count = 0; count < ship.getLength(); count++)
            {
                setShip(downX, y, ship.getType());
                y++;
            }
        }

        // check the position on the grid
        public int checkPosition(int x, int y, int size)
        {
            int direction = 3; // not placed
            int downX = x;

            // check down!
            bool okay = true;
            for (int count = 0; count < size; count++)
            {
                if (getShip(downX, y) != 0) okay = false;
                downX++;
            }
            //System.Windows.Forms.MessageBox.Show("Okay " + okay);  	  	 
            if (okay)
            {
                direction = 1;
            }

            // CHECK ACROSS
            if (!okay)
            {
                okay = true;
                for (int count = 0; count < size; count++)
                {
                    if (getShip(x, y) != 0) okay = false;
                    y++;
                }
                if (okay)
                {
                    direction = 2;
                }
            }
            return direction;
        }

        //setShip this sets what ship is hidden on the grid.
        public void setShip(int x, int y, int type)
        {
            //System.Windows.Forms.MessageBox.Show("Adding " + x +y + " type " + type);
            battleGrid[x, y] = type;
        }

        // initialise the grid
        public BattleGridManager()
        {
            Initialize();
        }

        // initialise the grid
        public void Initialize()
        {
            battleGrid = new int[15, 15] {
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

            missleGrid = new int[15, 15] {
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
						{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};

            // 0 Ocean 
            // 1 aircraft size 6
            // 2 destroyer size 5;
            // 3 cruiser size 4;
            // 4 submarine size 3
            // 5 Patrol ship size 2
            // 8 missle hit
            // 9 missle no hit

            // Tingo ***************
            //addShips();
        }
    }
}
